package com.filetransfer.view.Panel;
import com.filetransfer.config.UserThemeConfiguration;
import com.filetransfer.utils.ThemeLoader;
import de.exlll.configlib.YamlConfigurations;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.io.File;

import static com.filetransfer.Main.CONFIG_FILE_PATH;
@Slf4j
public class SettingPanel extends JPanel {
    private final ThemeLoader themeLoader;
    public SettingPanel() {
        themeLoader = new ThemeLoader();
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        UserThemeConfiguration config = YamlConfigurations.update(CONFIG_FILE_PATH, UserThemeConfiguration.class);
        JLabel titleLabel = new JLabel("设置界面", SwingConstants.CENTER);
        titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD, 16));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        gbc.anchor = GridBagConstraints.CENTER;
        add(titleLabel, gbc);
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        JLabel themeLabel = new JLabel("选择主题:");
        gbc.gridx = 0;
        gbc.gridy = 1;
        add(themeLabel, gbc);
        JComboBox<String> themeComboBox = new JComboBox<>();
        themeLoader.getThemeClasses().forEach(clazz -> themeComboBox.addItem(clazz.getSimpleName()));
        themeComboBox.setSelectedItem(config.getThemeName());
        themeComboBox.addActionListener(e -> {
            int selectedIndex = themeComboBox.getSelectedIndex();
            if (selectedIndex >= 0) {
                try {
                    String selectedTheme = themeComboBox.getSelectedItem().toString();
                    themeLoader.applyTheme(themeLoader.getThemeClasses().get(selectedIndex));
                    SwingUtilities.updateComponentTreeUI(SwingUtilities.getWindowAncestor(this));
                    config.setThemeName(selectedTheme);
                    YamlConfigurations.save(CONFIG_FILE_PATH, UserThemeConfiguration.class, config);
                    log.info("主题切换为: {}", selectedTheme);
                } catch (Exception ex) {
                    log.error("主题切换失败", ex);
                    JOptionPane.showMessageDialog(this, "主题切换失败！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        gbc.gridx = 1;
        add(themeComboBox, gbc);
        JLabel downloadPathLabel = new JLabel("下载路径:");
        gbc.gridx = 0;
        gbc.gridy = 2;
        add(downloadPathLabel, gbc);
        JTextField downloadPathField = new JTextField(config.getDownloadDirectory(), 20);
        downloadPathField.setEditable(false);
        gbc.gridx = 1;
        add(downloadPathField, gbc);
        JButton browseButton = new JButton("浏览");
        browseButton.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser(config.getDownloadDirectory());
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // 仅允许选择目录
            int returnValue = fileChooser.showOpenDialog(this);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedDir = fileChooser.getSelectedFile();
                downloadPathField.setText(selectedDir.getAbsolutePath());
                config.setDownloadDirectory(selectedDir.getAbsolutePath());
                YamlConfigurations.save(CONFIG_FILE_PATH, UserThemeConfiguration.class, config);
                log.info("下载路径更新为: {}", selectedDir.getAbsolutePath());
            }
        });
        gbc.gridx = 2;
        add(browseButton, gbc);
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 3;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        add(new JPanel(), gbc);
    }
}

